import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author: laoyu
 * @Date: 2020/6/2 12:00
 * @Description:
 */
public class TestHDFS {

    // 1、创建Configuration对象
    private static Configuration conf = new Configuration();
    // 2、设置配置参数：core-site.xml
    private static final String HDFSNAME = "fs.defaultFS";
    private static final String HDFSURI = "hdfs://localhost:9000";
    // 3、创建分布式文件系统对象
    private static FileSystem fs = null;


    /**
     * 初始化文件系统
     */
    private static FileSystem init() {
        conf.set(HDFSNAME, HDFSURI);
        try {
            fs = FileSystem.get(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fs;
    }

    static{
        init();
    }

    @Test
    public void testInit() {
        System.out.println(init());
    }

    /**
     * 读取文件
     */
    @Test
    public void readFile() {
        // 4、创建HDFS文件系统的输入流对象
        FSDataInputStream in = null;
        try {
            Path file = new Path("/test/word.txt");

            in = fs.open(file);
            // 文件流工具
            IOUtils.copyBytes(in, System.out, 4096, true);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeStream(in);
        }
    }

    /**
     * 创建目录
     */
    @Test
    public void createDir() throws IOException {
        //假定创建的文件是/hello/hi.txt
        String dir = HDFSURI+"/calc";
        Path path = new Path(dir);
        fs.mkdirs(path);
    }


    /**
     * 创建文件
     */
    @Test
    public void createFile() throws IOException {
        //假定创建的文件是/hello/hi.txt
        String file = "/hhh";
        Path path = new Path(file);
        FSDataOutputStream out = fs.create(path);
        InputStream in = new ByteArrayInputStream("Hello!Hadoop HDFS!".getBytes("utf-8"));
        IOUtils.copyBytes(in, out, 4096, true);
    }
    /**
     * 复制文件(上传)
     */
    @Test
    public void uploadFile() throws IOException {
        //文件名
        String fileName = "word.txt";
        //本地文件
        String srcFile = "/d:/"+fileName;
        //HDFS文件
        String destFile = "/calc/"+fileName;

        Path srcPath = new Path(srcFile);
        Path destPath = new Path(destFile);
        fs.copyFromLocalFile(srcPath,destPath);
    }

    /**
     * 复制文件(下载)
     */
    @Test
    public void downloadFile() throws IOException {
        Path srcPath = new Path("/hello/sayhi.txt");
        Path destPath = new Path("/d:/say.txt");
        fs.copyToLocalFile(srcPath,destPath);
    }

    /**
     * 删除文件
     */
    @Test
    public void deleteFile() throws IOException {
        fs.delete(new Path("/calc_output"),true);
    }

    /**
     * 重命名文件
     */
    @Test
    public void reNameFile() throws IOException {
        Path fromPath = new Path(HDFSURI+"/hhh");
        Path toPath = new Path(HDFSURI+"/rename.txt");
        fs.rename(fromPath,toPath);
    }

    /**
     * 查看已知文件
     */
    @Test
    public void getFileStauts() throws IOException {
        Path file = new Path(HDFSURI+"/rename.txt");
        FileStatus status = fs.getFileStatus(file);
        System.out.println(status);
        System.out.println("===============================");
        System.out.println(status.getPath());
        System.out.println(status.getBlockSize());
        System.out.println(status.getLen());
        System.out.println(status.getReplication());
        System.out.println(status.getOwner());
        System.out.println(status.getGroup());
        System.out.println(status.getPermission().toString());
    }

    /**
     * 查看某目录下的所有文件
     */
    @Test
    public void getListStauts() throws IOException {
        Path file = new Path(HDFSURI+"/hello");
        FileStatus[] fileStatuses = fs.listStatus(file);
        for (FileStatus status : fileStatuses) {
            System.out.println("===============================");
            System.out.println(status.getPath());
            System.out.println(status.getBlockSize());
            System.out.println(status.getLen());
            System.out.println(status.getReplication());
            System.out.println(status.getOwner());
            System.out.println(status.getGroup());
            System.out.println(status.getPermission().toString());
        }
    }

    /**
     * 判断文件或目录是否存在
     */
    @Test
    public void fileIsExists() throws IOException {
        Path file = new Path(HDFSURI+"/hello");
        boolean exists = fs.exists(file);
        if (exists) {
            System.out.println("存在");
        }else{
            System.out.println("不存在");
        }
    }


}
